package com.birds.bird_05_;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.test.suitebuilder.TestMethod;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private ImageView compassImg;
    private TextView text;
    private FrameLayout warning;
    private SensorManager sensorManager;
    private SensorEventListener listener = new SensorEventListener() {

        float[] accelerometerValues = new float[3];

        float[] magneticValues = new float[3];

        private float lastRotateDegree;

        @Override
        public void onSensorChanged(SensorEvent event) {


           /* if(false){
                float scaleX,scaleY;
                int speed = 3;
                Date time = new Date();
                scaleX = 1 + (float) Math.cos(time.getTime()
                        / Math.pow(10,speed) % 10 * 0.1 * 2 * Math.PI);
                scaleY = 1 + (float) Math.sin(time.getTime()
                        / Math.pow(10,speed) % 10 * 0.1 * 2 * Math.PI);
                compassImg.setScaleY(scaleY);
                compassImg.setScaleX(scaleX);
            }*/

            // 判断当前是加速度传感器还是地磁传感器
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                // 注意赋值时要调用clone()方法
                accelerometerValues = event.values.clone();
            } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                // 注意赋值时要调用clone()方法
                magneticValues = event.values.clone();
            }

//            判断手机是否水平
            Animation fadeIn = new AlphaAnimation(1.0f,0f);
            fadeIn.setDuration(3000);
            Animation fadeOut = new AlphaAnimation(0f,1.0f);
            fadeOut.setDuration(3000);

            if(Math.abs(accelerometerValues[0]) < 2 && Math.abs(accelerometerValues[1]) < 2){
//                warning.setVisibility(View.INVISIBLE);
//                warning.startAnimation(fadeOut);
                text.setVisibility(View.INVISIBLE);
            }else{
//                warning.startAnimation(fadeIn);
//                warning.setVisibility(View.VISIBLE);
                text.setVisibility(View.VISIBLE);
            }

            //记录旋转角度
            float[] values = new float[3];
            float[] R = new float[9];
            SensorManager.getRotationMatrix(R, null, accelerometerValues,
                    magneticValues);
            SensorManager.getOrientation(R, values);
            float rotateDegree = -(float) Math.toDegrees(values[0]);
            if (Math.abs(rotateDegree - lastRotateDegree) > 5) {
                RotateAnimation animation = new RotateAnimation(
                        lastRotateDegree, rotateDegree,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                animation.setFillAfter(true);
                compassImg.startAnimation(animation);
                lastRotateDegree = rotateDegree;
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        compassImg = (ImageView) findViewById(R.id.compass_img);
        text = (TextView) findViewById(R.id.text);
//        warning = (FrameLayout) findViewById(R.id.warning);

        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        Sensor magneticSensor = sensorManager
                .getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        Sensor accelerometerSensor = sensorManager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(listener, magneticSensor,
                SensorManager.SENSOR_DELAY_GAME);
        sensorManager.registerListener(listener, accelerometerSensor,
                SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (sensorManager != null) {
            sensorManager.unregisterListener(listener);
        }
    }


}
